//
//  UUInputView.swift
//  驻外机组
//
//  Created by 苏伟 on 16/3/28.
//  Copyright © 2016年 com.csair. All rights reserved.
//


import UIKit

typealias TextBlock  = (text:String, textView:UITextView)->Void

class UUInputView: UIView,UITextViewDelegate{

    @IBOutlet weak var leftButton: UIButton!
    
    @IBOutlet weak var rightButton: UIButton!
    var sendTextBlock:TextBlock!

    
    @IBOutlet weak var contentTextView: UITextView!
    @IBOutlet weak var contentViewHeightConstraint:NSLayoutConstraint!
    
    var placeHolderLabel: UILabel!
    
    @IBAction func sendmsgclicked(sender: AnyObject) {
        if contentTextView.text == ""
        {return}
        self.sendTextBlock!(text: contentTextView.text!, textView: contentTextView)
        contentTextView.text = ""
        //设置输入框大小调整
        self.textViewDidChange(contentTextView)
    }
    

    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.borderWidth = 1;
        self.layer.borderColor = UIColor.blackColor().colorWithAlphaComponent(0.1).CGColor
        self.contentTextView.delegate = self
        self.contentTextView.returnKeyType = .Send
        self.contentTextView.enablesReturnKeyAutomatically = true
        
        placeHolderLabel = UILabel()
        placeHolderLabel.text = "点击输入要发送的消息"
        placeHolderLabel.textColor = UIColor.lightGrayColor()
        placeHolderLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
        contentTextView.addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentTextView)
        }
        
       
    }
    
    func sendMessage(textBlock:TextBlock){
        self.sendTextBlock = textBlock
    }
    
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if text != "\n" {
            return true
        } else {
            // send text
            self.sendTextBlock!(text: contentTextView.text!, textView: contentTextView)
            textView.text = ""
            self.textViewDidChange(textView)
            return false
        }
    }

    
    //MARK: textViewDelegate
    func textViewDidBeginEditing(textView: UITextView) {
        placeHolderLabel.hidden = true
    }
    
    func textViewDidEndEditing(textView: UITextView) {
        placeHolderLabel.hidden = !textView.text.isEmpty
    }

    
    
    // adjust content's height from 40 t0 100
    func textViewDidChange(textView: UITextView) {
        let textContentH = textView.contentSize.height
        print("output：\(textContentH)")
        let textHeight = textContentH>40 ? (textContentH<100 ? textContentH:100):40
        UIView.animateWithDuration(0.2) { () -> Void in
            self.contentViewHeightConstraint.constant = textHeight
            self.layoutIfNeeded()
            self.superview?.layoutIfNeeded()
            let vc = self.responderViewController() as! UUChatViewController
            vc.view.layoutIfNeeded()
            vc.chatTableView.scrollToBottom(animation: false)
        }
    }
    
   
    
}
